<template>
  <el-card shadow="always" :body-style="{ padding: '20px' }">


    <div class="feedback-container">
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span>反馈列表</span>
          </div>
        </template>
        <!-- 搜索区域 -->
        <div class="search-container">
          <el-form :inline="true" :model="searchForm" class="search-form">
            <el-form-item label="关键字">
              <el-input v-model="searchForm.keyword" placeholder="请输入标题或内容" clearable @keyup.enter="handleSearch" />
            </el-form-item>
            <el-form-item label="状态">
              <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
                <el-option label="未采纳" :value="0" />
                <el-option label="已采纳" :value="1" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSearch">搜索</el-button>
              <el-button @click="handleReset">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
        <el-table v-loading="loading" :data="tableData" style="width: 100%"
          :header-cell-style="{ background: '#f5f7fa', color: '#606266' }" border stripe>
          <el-table-column prop="userId" label="用户ID" width="100" align="center" />
          <el-table-column prop="userName" label="用户名" width="120" align="center" />
          <el-table-column prop="title" label="标题" width="200" align="center" />
          <el-table-column prop="content" label="内容" align="center" show-overflow-tooltip>
            <template #default="{ row }">
              <span class="content-text">{{ row.content }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="createdTime" label="反馈时间" width="180" align="center" />
          <el-table-column prop="status" label="状态" width="100" align="center">
            <template #default="{ row }">
              <el-tag :type="row.status == 1 ? 'success' : 'warning'" effect="dark" class="status-tag">
                {{ row.status == 1 ? '已采纳' : '未采纳' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="200" align="center" fixed="right">
            <template #default="{ row }">
              <el-button type="primary" size="small" @click="handleView(row)">
                查看
              </el-button>
              <el-button v-if="row.status == 0" type="success" size="small" @click="handleResolve(row)">
                采纳
              </el-button>
              <!-- <el-button v-else  type="warning" size="small"
                @click="handleUnresolve(row)">
                未采纳
              </el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination-container">
          <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[10, 20, 50, 100]"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"
            layout="total, sizes, prev, pager, next" background />
        </div>
      </el-card>

      <!-- 查看详情对话框 -->
      <el-dialog v-model="dialogVisible" title="反馈详情" width="600px" :close-on-click-modal="false"
        class="feedback-dialog">
        <div class="detail-content">
          <div class="detail-section">
            <div class="detail-item">
              <span class="label">用户信息</span>
              <div class="value-group">
                <div class="value-item">
                  <span class="sub-label">用户ID：</span>
                  <span class="sub-value">{{ currentFeedback.userId }}</span>
                </div>
                <div class="value-item">
                  <span class="sub-label">用户名：</span>
                  <span class="sub-value">{{ currentFeedback.userName }}</span>
                </div>
              </div>
            </div>
            <div class="detail-item">
              <span class="label">反馈信息</span>
              <div class="value-group">
                <div class="value-item">
                  <span class="sub-label">标题：</span>
                  <span class="sub-value">{{ currentFeedback.title }}</span>
                </div>
                <div class="value-item">
                  <span class="sub-label">内容：</span>
                  <div class="content-box">
                    {{ currentFeedback.content }}
                  </div>
                </div>
              </div>
            </div>
            <div class="detail-item">
              <span class="label">其他信息</span>
              <div class="value-group">
                <div class="value-item">
                  <span class="sub-label">反馈时间：</span>
                  <span class="sub-value">{{ currentFeedback.createdTime }}</span>
                </div>
                <div class="value-item">
                  <span class="sub-label">状态：</span>
                  <el-tag :type="currentFeedback.status == 1 ? 'success' : 'warning'" effect="dark" class="status-tag">
                    {{ currentFeedback.status == 1 ? '已采纳' : '未采纳' }}
                  </el-tag>
                </div>
              </div>
            </div>
          </div>
        </div>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="dialogVisible = false">关闭</el-button>
          </span>
        </template>
      </el-dialog>

      <!-- 反馈类型选择弹窗 -->
      <el-dialog v-model="resolveDialogVisible" title="选择反馈类型" width="700px" :close-on-click-modal="false">
        <el-form :model="resolveForm" label-width="100px">
          <el-form-item label="反馈类型：">
            <el-checkbox-group v-model="resolveForm.sort" :min="1" :max="1">
              <el-checkbox v-for="item in feedbackTypes" :key="item.value" :label="item.value">
                {{ item.label }}
              </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="resolveDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="confirmResolve">确定</el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </el-card>

</template>

<script setup>
import { ref, onMounted } from 'vue'
import { feedbackList, feedbackUpdate } from '@/api/zhaokao/feedback'
import { ElMessage, ElMessageBox } from 'element-plus'

const loading = ref(false)
const tableData = ref([])
const total = ref(0)
const pageNum = ref(1)
const pageSize = ref(10)
const dialogVisible = ref(false)
const currentFeedback = ref({})

const searchForm = ref({
  keyword: '',
  status: ''
})

const handleSearch = () => {
  pageNum.value = 1 // 重置页码
  getList()
}

const handleReset = () => {
  searchForm.value = {
    keyword: '',
    status: ''
  }
  handleSearch()
}

const getList = async () => {
  loading.value = true
  try {
    const res = await feedbackList({
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      keyword: searchForm.value.keyword,
      status: searchForm.value.status
    })
    if (res.code === 200) {
      tableData.value = res.data.records
      total.value = res.data.total
    } else {
      ElMessage.error(res.msg || '获取数据失败')
    }
  } catch (error) {
    ElMessage.error('获取数据失败')
    console.error('获取反馈列表失败:', error)
  } finally {
    loading.value = false
  }
}

const handleSizeChange = (val) => {
  pageSize.value = val
  pageNum.value = 1
  getList()
}

const handleCurrentChange = (val) => {
  pageNum.value = val
  getList()
}

const handleView = (row) => {
  currentFeedback.value = { ...row }
  dialogVisible.value = true
}

const resolveDialogVisible = ref(false)
const resolveForm = ref({
  sorts: []
})

const feedbackTypes = [
  { label: '错别字、病句、排版', value: '1' },
  { label: '功能、运营', value: '2' },
]

const handleResolve = (row) => {
  resolveForm.value = {
    sort: [],
    row
  }
  resolveDialogVisible.value = true
}

const confirmResolve = async () => {
  if (resolveForm.value.sort.length === 0) {
    ElMessage.warning('请至少选择一个反馈类型')
    return
  }

  try {
    const res = await feedbackUpdate({
      id: resolveForm.value.row.id,
      status: 1,
      sort: resolveForm.value.sort[0],
      userId: resolveForm.value.row.userId
    })
    if (res.code === 200) {
      ElMessage.success('操作成功')
      resolveDialogVisible.value = false
      getList() // 刷新列表
    } else {
      ElMessage.error(res.msg || '操作失败')
    }
  } catch (error) {
    ElMessage.error('操作失败')
    console.error('更新反馈状态失败:', error)
  }
}

const handleUnresolve = (row) => {
  ElMessageBox.confirm(
    '确认将该反馈标记为未解决吗？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(async () => {
    try {
      const res = await feedbackUpdate({
        id: row.id,
        status: 0
      })
      if (res.code === 200) {
        ElMessage.success('操作成功')
        getList() // 刷新列表
      } else {
        ElMessage.error(res.msg || '操作失败')
      }
    } catch (error) {
      ElMessage.error('操作失败')
      console.error('更新反馈状态失败:', error)
    }
  }).catch(() => { })
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
.feedback-container {}

.box-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 16px;
  font-weight: bold;
  color: #303133;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.status-tag {
  min-width: 60px;
  text-align: center;
}

:deep(.el-table th) {
  background-color: #f5f7fa !important;
  font-weight: 600;
}

:deep(.el-table--striped .el-table__body tr.el-table__row--striped td) {
  background: #fafafa;
}

.content-text {
  display: inline-block;
  max-width: 300px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.el-button {
  margin: 0 4px;
}

.feedback-dialog :deep(.el-dialog__body) {
  padding: 20px 30px;
}

.detail-section {
  background: #f8f9fa;
  border-radius: 8px;
  padding: 20px;
}

.detail-item {
  margin-bottom: 24px;
}

.detail-item:last-child {
  margin-bottom: 0;
}

.detail-item .label {
  display: block;
  font-size: 16px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 16px;
  padding-left: 8px;
  border-left: 4px solid #409EFF;
}

.value-group {
  background: #fff;
  border-radius: 6px;
  padding: 16px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.value-item {
  margin-bottom: 16px;
  display: flex;
  align-items: flex-start;
}

.value-item:last-child {
  margin-bottom: 0;
}

.sub-label {
  width: 80px;
  color: #606266;
  font-weight: 500;
}

.sub-value {
  flex: 1;
  color: #303133;
}

.content-box {
  flex: 1;
  background: #f8f9fa;
  padding: 12px;
  border-radius: 4px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  padding-top: 20px;
}

.search-container {
  margin-bottom: 20px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

:deep(.el-form-item) {
  margin-bottom: 0;
}

:deep(.el-input) {
  width: 200px;
}

:deep(.el-select) {
  width: 200px;
}
</style>
